package cn.itcast.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.itcast.dao.MenuDao;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.pojo.Menu;
import cn.itcast.pojo.Permission;
import cn.itcast.service.MenuService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    @Override
    public List<Menu> queryAll() {
        List<Menu> page = this.menuDao.queryAll();
        return page;
    }

    @Override
    public PageResult queryByPage(QueryPageBean queryPageBean) {

        if (StrUtil.isNotEmpty(queryPageBean.getQueryString())) {
            Page<Object> objects = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
            this.menuDao.queryByCondition(Convert.toInt(queryPageBean.getQueryString()));
            PageInfo pageInfo = new PageInfo(objects);
            return new PageResult(pageInfo.getTotal(), pageInfo.getList());
        }

        //开启分页查询
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //根据条件分页查询检查项信息
        Page<Menu> page = menuDao.querybyPage();
        //构建分页数据返回
        return new PageResult(page.getTotal(), page.getResult());

    }

    @Override
    public void saveMenu(Menu menu, Integer[] permissionIds) {

        this.menuDao.saveMenu(menu);

        //判断是否带有权限id
        if (ArrayUtil.isNotEmpty(permissionIds)) {
            //查询新增数据
            Menu queryName = this.menuDao.queryByName(menu.getName());
            for (Integer permissionId : permissionIds) {
                this.menuDao.saveMenuToPermission(queryName.getId(), permissionId);
            }
        }
    }

    @Override
    public Menu queryById(Integer id) {
        return this.menuDao.queryById(id);
    }


    @Override
    public Integer[] queryMenuPermission(Integer id) {
        List<Permission> page = this.menuDao.queryMenuPermission(id);
        Integer[] arr = new Integer[page.size()];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = page.get(i).getId();
        }
        return arr;
    }

    @Override
    public void updateMenu(Menu menu, Integer[] permissionIds) {
        this.menuDao.update(menu);

        //删除原有权限与菜单关系
        this.menuDao.deleteMenuToPermission(menu.getId());
        //判断是否带有权限id
        if (ArrayUtil.isNotEmpty(permissionIds)) {
            for (Integer permissionId : permissionIds) {
                //保存新的关系
                this.menuDao.saveMenuToPermission(menu.getId(), permissionId);
            }
        }
    }

    @Override
    public Boolean deleteById(Integer id) {

        //判断是否有关联数据
        if (this.menuDao.queryByRole(id) > 0 || this.menuDao.queryByPermission(id) > 0) {
            return false;
        }


        //查询是否有子集菜单
        List<Menu> menuList = this.menuDao.queryByCondition(id);
        if (CollUtil.isNotEmpty(menuList)) {
            //判断是否有关联数据
            for (Menu menu : menuList) {
                if (this.menuDao.queryByRole(menu.getId()) > 0 || this.menuDao.queryByPermission(menu.getId()) > 0) {
                    return false;
                }
            }
        }
        return this.menuDao.deleteById(id) > 0;
    }

    @Override
    public ArrayList<Menu> getMenuList(String username) {
        List<Menu> parentMenu = menuDao.getParentMenu(username);
        ArrayList<Menu> menuList =new ArrayList<>();
        for (Menu menu : parentMenu) {
            List<Menu> childrenMenu = menuDao.getChildrenMenuByParentMenuId(username, menu.getId());
            menu.setChildren(childrenMenu);
            menuList.add(menu);
        }
        return menuList;
    }
}
